package com.example.todolist.service;

import com.example.todolist.entity.Mail;
import com.example.todolist.entity.User;
import com.example.todolist.mapper.UserMapper;
import com.example.todolist.tools.ResponseBody;
import com.example.todolist.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class UserService {
    @Autowired
    private VerificationService verificationService;
    @Autowired
    private MailService mailService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TaskEachDayService taskEachDayService;
    public ResponseBody<Integer> login(String name, String password)
    {
        User user=userMapper.login(name,password);
        if(user==null)
        {
            return new ResponseBody<>(-1,"the password is wrong",null);
        }
        taskEachDayService.setTaskEachDay(user.getUserId());
        String token= TokenUtils.genToken(user.getUserId().toString());
        return new ResponseBody<>(200,token, user.getUserId());
    }

    public ResponseBody<Integer> register(User user)
    {
        User us=userMapper.findByName(user.getName());
        if(us!=null)
        {
            return new ResponseBody<>(-1,"the name exists",null);
        }
        Random r=new Random();
        int number=r.nextInt(899999);
        number+=100000;
        verificationService.Insert(user.getName(),number);
        Mail mail=new Mail("[TomatoDo]您的验证码为： "+Integer.toString(number)+"，如非您本人操作，请忽略本消息",user.getEmail(),"TomatoDo");
        mailService.sendMailMessage(mail);
        return new ResponseBody<>(200,"请输入验证码",1);
    }

    public ResponseBody<Integer> registerConfirm(User user,Integer number)
    {
        if(!number.equals(verificationService.getTheNumber(user.getName())))
        {
            return new ResponseBody<>(-1,Integer.toString(number),verificationService.getTheNumber(user.getName()));
        }
        userMapper.register(user);
        verificationService.DeleteTheNumber(user.getName());
        int data=userMapper.findByName(user.getName()).getUserId();
        return new ResponseBody<>(200,"success",data);
    }

    public ResponseBody<Integer> updateEmail(Integer userId,String email)
    {
        User user=userMapper.findByUserId(userId);
        if(user==null)
        {
            return new ResponseBody<>(-1,"the id does not exist",null);
        }
        userMapper.updateEmail(userId,email);
        return new ResponseBody<>(200,"success",userId);
    }

    public ResponseBody<Integer> updatePassword(Integer userId,String password)
    {
        User user=userMapper.findByUserId(userId);
        if(user==null)
        {
            return new ResponseBody<>(-1,"the id does not exist",null);
        }
        userMapper.updatePassword(userId,password);
        return new ResponseBody<>(200,"success",userId);
    }

    private List<User> index()                 //查找所有用户信息
    {
        return userMapper.findAll();
    }

    public ResponseBody<User> findByUserId(Integer userId)
    {
        User user=userMapper.findByUserId(userId);
        if(user==null)
        {
            return new ResponseBody<>(-1,"the id does not exist",null);
        }
        return new ResponseBody<>(200,"success",user);
    }
}
